package com.cysyz.mylib.excel.engine.impl.poi2;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.cysyz.mylib.excel.engine.ExcelEngine;
import com.cysyz.mylib.excel.engine.SheetEngine;
import com.cysyz.mylib.excel.exception.ExcelException;

public class ExcelEnginePoi2Empl implements ExcelEngine{
	private HSSFWorkbook workbook;
	
	public ExcelEnginePoi2Empl(File excelFile) throws ExcelException {
		super();
		List<String> errorList = new ArrayList<String>();
		try {
			this.workbook = new HSSFWorkbook(new FileInputStream(excelFile));
		} catch (FileNotFoundException e) {
			errorList.add("在【" +excelFile.getAbsolutePath() + "】下无法找到指定文件");
			throw new ExcelException(errorList );
		} catch (IOException e) {
			errorList.add("文件传输过程中发生意外，请重试");
			throw new ExcelException(errorList );
		}
	}

	@Override
	public SheetEngine getSheet(String sheetName) {
		return new SheetEnginePoi2Impl(workbook, workbook.getSheet(sheetName), workbook.getSheetIndex(sheetName));
	}

	@Override
	public SheetEngine getSheet(int sheetIdx) {
		return new SheetEnginePoi2Impl(workbook, workbook.getSheetAt(sheetIdx), sheetIdx);
	}

	@Override
	public int getNumberOfSheets() {
		return workbook.getNumberOfSheets();
	}

}
